<template>
  <div class="container">
    <div class="app-container">
      <div class="bt">
        <el-button
          type="praimary"
          size="mini"
          @click="showDialog = true"
        >
          添加角色
        </el-button>
      </div>
      <el-table
        :data="tableData"
        style="width: 100%"
      >
        <el-table-column
          prop="name"
          label="角色"
          width="200"
          align="center"
        >
          <template v-slot="{ row }">
            <!-- 条件判断 -->
            <el-input v-if="row.isEdit" v-model="row.editRows.name" size="mini" />
            <span v-else>{{ row.name }}</span>
          </template></el-table-column>
        <el-table-column
          label="启用"
          width="200"
          align="center"
        >
          <!-- 添加默认插槽将数据存入，然后判断 -->
          <template v-slot="{row}">
            <el-switch
              v-if="row.isEdit"
              v-model="row.editRows.state"
              :inactive-value="0"
              :active-value="1"
            />
            <span>{{ row.state===1?'启用':row.state===0?'未启用':'无' }}</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="description"
          label="描述"
          align="center"
        >
          <template v-slot="{ row }">
            <!-- 条件判断 -->
            <el-input v-if="row.isEdit" v-model="row.editRows.description" size="mini" type="textarea" />
            <span v-else>{{ row.description }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          align="center"
        >
          <template v-slot="{ row }">
            <template v-if="row.isEdit">
              <el-button type="primary" size="mini" @click="qdxg(row)">确定</el-button>
              <el-button size="mini" @click="row.isEdit=false">取消</el-button>
            </template>
            <template v-else>
              <el-button type="text" size="mini">分配权限</el-button>
              <el-button type="text" size="mini" @click="edit(row)">编辑</el-button>
              <el-popconfirm
                title="这是一段内容确定删除吗？"
                @onConfirm="delConfirm(row.id)"
              >
                <el-button slot="reference" type="text" size="mini">删除</el-button>
              </el-popconfirm>
            </template>
          </template>
        </el-table-column>
      </el-table>
      <el-row type="flex" justify="end" style="height:60px,align:middel">
        <el-pagination
          layout="prev,pager,next"
          :total="pageParams.total"
          :page-size="pageParams.pagesize"
          :current-page="pageParams.page"
          @current-change="currentChangeFn"
        />
      </el-row>
      <el-dialog width="500px" title="新增角色" :visible.sync="showDialog">
        <!-- 表单内容 -->
        <el-form ref="addForm" label-width="120px" :model="addForm" :rules="rules" @close="closeFn">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="addForm.name" style="width:300px" size="mini" />
          </el-form-item>
          <el-form-item label="启用">
            <el-switch
              v-model="addForm.state"
              :inactive-value="0"
              :active-value="1"
            />
          </el-form-item>
          <el-form-item label="角色描述" prop="description">
            <el-input v-model="addForm.description" type="textarea" :rows="3" style="width:300px" size="mini" placeholder="请输入内容" />
          </el-form-item>
          <el-form-item>
            <el-row type="flex" justify="center">
              <el-col :span="12">
                <el-button type="primary" size="mini" @click="submit">确定</el-button>
                <el-button size="mini" @click="closeFn">取消</el-button>
              </el-col>
            </el-row>
          </el-form-item>
        </el-form>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { getRoleList, addRole, upDatarole, delRole } from '@/api/role'
export default {
  name: 'Role',
  data() {
    return {
      tableData: [],
      pageParams: {
        page: 1,
        pagesize: 5,
        total: 0
      },
      showDialog: false,
      value: '',
      addForm: {
        name: '',
        description: '',
        state: 1
      },
      rules: {
        name: [{ required: true, message: '内容不能为空', blur: true }],
        description: [{ required: true, message: '内容不能为空', blur: true }]
      }
    }
  },
  created() {
    this.getRoleList()
  },
  methods: {
    async getRoleList() {
      const { rows, total } = await getRoleList(this.pageParams)
      // console.log(res)
      this.pageParams.total = total
      this.tableData = rows
      // 针对每一行数据添加一个编辑标记
      this.tableData.forEach(item => {
        // item.  = false // 添加一个属性 初始值为false
        // 数据响应式的问题  数据变化 视图更新
        // 添加的动态属性 不具备响应式特点
        // this.$set(目标对象, 属性名称, 初始值) 可以针对目标对象 添加的属性 添加响应式
        this.$set(item, 'isEdit', false)
        this.$set(item, 'editRows', {
          name: item.name,
          state: item.state,
          description: item.description
        })
      })
    },
    async qdxg(row) {
      if (!row.editRows.name || !row.editRows.description) {
        this.$message.warning('角色或者描述不能为空')
      } else {
        await upDatarole({ ...row.editRows, id: row.id })
        this.$message.success('修改成功')
        this.getRoleList()
        // eslint-disable-next-line space-infix-ops
        // this.isEdit=false
        // 第一个为修复第二个使用
        // assign内有两个对象如果两个对象名相同，后一个会覆盖前面一个
        // 所以此时改变了row里面的值
        Object.assign(row, {
          ...row.editRows,
          isEdit: false
        })
      }
    },
    currentChangeFn(page) {
      this.pageParams.page = page
      this.getRoleList()
    },
    submit() {
      this.$refs.addForm.validate(async pass => {
        if (pass) {
          await addRole({ ...this.addForm })
          this.$message.success('添加成功')
          this.showDialog = false
          this.getRoleList()
          this.closeFn()
        }
      })
    },
    closeFn() {
      this.$refs.addForm.resetFields()
      this.showDialog = false
    },
    // 删除角色
    async delConfirm(id) {
      await delRole(id)
      this.$message.success('删除成功')
      // 判断如果是当前页最后一个的后就剪掉当前页，在调用
      if (this.tableData.length === 1) {
        this.pageParams.page--
      }
      this.getRoleList()
    },
    edit(row) {
      row.isEdit = true
      row.editRows.name = row.name
      row.editRows.description = row.description
      row.editRows.state = row.state
    }

  }
}
</script>
<style lang="scss">
.el-button{
  color: black;
}
</style>
